package com.wangxiaonan.berry.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wangxiaonan.berry.entity.TongDun;
import com.wangxiaonan.berry.repository.TongDunRepository;
import com.wangxiaonan.berry.service.TongDunService;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * @Author: wangxiaonan
 * @Date: 2019/7/3
 **/
@Service
public class TongDunServiceImpl implements TongDunService {

    @Value("${tong-dun-secret-key}")
    private String secretKey;

    @Value("${tong-dun-partner-code}")
    private String partnerCode;

    @Resource
    private TongDunRepository tongDunRepository;

    @Override
    public String riskService(TongDun tongdun) {
        Assert.notNull(tongdun, "Parameter is null!");
        if (StringUtils.isEmpty(tongdun.getSecretKey())) {
            tongdun.setSecretKey(secretKey);
        }
        if (StringUtils.isEmpty(tongdun.getPartnerCode())) {
            tongdun.setPartnerCode(partnerCode);
        }
        if (StringUtils.isEmpty(tongdun.getRespDetailType())) {
            tongdun.setRespDetailType("device");
        }
        Map<String, String> queryMap = new HashMap<>();
        queryMap.put("partner_code", tongdun.getPartnerCode());
        queryMap.put("secret_key", tongdun.getSecretKey());
        queryMap.put("event_id", tongdun.getEventId());
        queryMap.put("token_id", tongdun.getTokenId());
        queryMap.put("black_box", tongdun.getBlackBox());
        queryMap.put("resp_detail_type", tongdun.getRespDetailType());
        queryMap.put("refer_cust", tongdun.getReferCust());
        queryMap.put("event_occur_time", tongdun.getEventOccurTime());

        JSONObject jsonObject = tongDunRepository.riskService(queryMap);
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }
        return jsonObject.toJSONString();
    }
}
